﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using XMK.Framework.DataAccess;
using XMK.Tadong.Model;
using XMK.Utils;
using XMK.Framework.Model;
using XMK.Tadong.Model.Cond;
namespace XMK.Tadong.Data
{
  public  class TB_TEAM_USER_userData
    {
      /// <summary>
        /// 数据库连接
        /// </summary>
        protected DbProvider dbProvider;

        public TB_TEAM_USER_userData(DbProvider dbProvider)
        {
            this.dbProvider = dbProvider;

        }
        /// <summary>
        /// 判断是否已有
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public bool IsExist(TB_TEAM_USERInfo Obj)
        {
            decimal teamid = Obj.TEAM_ID;
            string userid = Obj.USER_ID;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT count(*) FROM TB_TEAM_USER WHERE TEAM_ID = '" + teamid + "' and USER_ID='" + userid + "'");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            object obj = DbManager.ExecScalar(cmd);

            return (Convert.ToInt16(obj) != 0);


        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public bool InsertNewTeam(TB_TEAM_USERInfo Obj)
        {
            bool result = true;
            string sql = @"insert into USER_ID(TEAM_ID, USER_ID,CAN_SCAN ,CAN_DOWN ,CAN_UPLOAD,  CAN_DELETE,CREATE_TIME,UPD_TIME)
                                             values(:p_TEAM_ID,:p_USER_ID,:p_CAN_SCAN, :p_CAN_DOWN, :p_CAN_UPLOAD, :p_CREATE_TIME,:p_CREATE_TIME,:p_UPD_TIME)";

            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql,
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "TEAM_ID", "USER_ID", "CAN_SCAN", "CAN_DOWN", "CAN_UPLOAD", "CAN_DELETE","CREATE_TIME","UPD_TIME").ToArray();
                });
            //参数设值
            cmd.SetParamValues(Obj.TEAM_ID, Obj.USER_ID, Obj.CAN_SCAN, Obj.CAN_DOWN, Obj.CAN_UPLOAD, Obj.CAN_DELETE, Obj.CREATE_TIME, Obj.UPD_TIME);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            int back = DbManager.ExecNonQuery(cmd);
            if (back == 0)
                result = false;

            return result;

        }
        /// <summary>
        /// 根据用户ID获取课题组列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetTeamListByUserId(string userId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM TB_TEAM_USER WHERE USER_ID = '" + userId + "'");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;
        }


        /// <summary> 
        /// 根据用户ID获取课题组列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetTeamDetailListByUserId(string userId)
        {
            StringBuilder strSql = new StringBuilder();//
            // strSql.Append("SELECT TB_TEAM_USER.*,TB_TEAM.TEAM_ID TEAM_ID, TB_TEAM.TEAM_NAME TEAM_NAME,TB_TEAM.TEAM_LEADER TEAM_LEADER，TB_TEAM.TEAM_STS TEAM_STS FROM TB_TEAM_USER,TB_TEAM WHERE USER_ID = '" + userId + "' and TB_TEAM.TEAM_ID=TB_TEAM_USER.TEAM_ID ");
           // strSql.Append("SELECT TB_TEAM.TEAM_ID TEAM_ID, TB_TEAM.TEAM_NAME TEAM_NAME,TB_TEAM.TEAM_LEADER TEAM_LEADER，TB_TEAM.TEAM_STS TEAM_STS FROM  tb_team where team_id in (select team_id from tb_team_user where user_id ='" + userId + "') ");// or team_name='行政资料管理'
            strSql.Append("SELECT TB_TEAM.TEAM_ID , TB_TEAM.TEAM_NAME ,TB_TEAM.TEAM_LEADER ，TB_TEAM.TEAM_STS,tb_team_user.dataadmin FROM  tb_team inner join tb_team_user on tb_team.team_id=tb_team_user.team_id  where tb_team_user.user_id ='" + userId+"'");// or team_name='行政资料管理'
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;
        }

      
    }
}
